﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DLL;
using BLL;

namespace QLCuaHang
{
    public partial class frmNguoiDung : Form
    {
        //PhieuXuatBLL px = new PhieuXuatBLL();
        NhanVienBLL NV = new NhanVienBLL();
        NguoiDungBLL nd = new NguoiDungBLL();
        DangNhapBLL DN = new DangNhapBLL();
        public frmNguoiDung()
        {
            InitializeComponent();
        }

        private void frmNguoiDung_Load(object sender, EventArgs e)
        {
            if (frmDangNhap.quyen == 2)
            {
                
                btnSua.Enabled = false;
                
            }
            LoadCbbMaNv();
            LoadCbbQuyen();
            GridConfig();
            grvNguoidung.DataSource = nd.Select_ALL();
        }
        void LoadCbbMaNv()
        {
            for (int i = 0; i < NV.SelectALL_MaNv().Rows.Count; i++)
            {
                NhanVienDAL nv = new NhanVienDAL();
                nv.MaNv = NV.SelectALL_MaNv().Rows[i]["MaNv"].ToString();
                nv.TenNV = NV.SelectALL_MaNv().Rows[i]["TenNV"].ToString();
                nv.HoDem = NV.SelectALL_MaNv().Rows[i]["HoDem"].ToString();
                nv.NS = DateTime.Parse(NV.SelectALL_MaNv().Rows[i]["NS"].ToString());
                nv.NG_VaoLam = DateTime.Parse(NV.SelectALL_MaNv().Rows[i]["NG_VaoLam"].ToString());
                nv.DiaChi = NV.SelectALL_MaNv().Rows[i]["DiaChi"].ToString();
                nv.DienThoai = NV.SelectALL_MaNv().Rows[i]["MaNV"].ToString();
                nv.Luong = float.Parse(NV.SelectALL_MaNv().Rows[i]["Luong"].ToString());
                nv.EMail = NV.SelectALL_MaNv().Rows[i]["Mail"].ToString();
                nv.GT = NV.SelectALL_MaNv().Rows[i]["GT"].ToString();

                cbbMaNv.Items.Add(nv);
            }
            cbbMaNv.DisplayMember = "MaNv";
            cbbMaNv.SelectedIndex = 0;

        }
        void LoadCbbQuyen()
        {
            for (int i = 0; i < DN.Select_ALL().Rows.Count; i++)
            {
                DangNhapDLL dn = new DangNhapDLL();
                dn.MaQuyen = int.Parse(DN.Select_ALL().Rows[i]["MaQuyen"].ToString());
                dn.TenQuyen = DN.Select_ALL().Rows[i]["TenQuyen"].ToString();
                cbbQuyen.Items.Add(dn);
            }
            cbbQuyen.DisplayMember = "TenQuyen";
            cbbQuyen.SelectedIndex = 0;
        }
        private void GridConfig()
        {
            grvNguoidung.ModeColorRow = 1;
            grvNguoidung.SetColorRow(System.Drawing.Color.AliceBlue);
            grvNguoidung.AddColumns("TextBox", "MaNv", "Mã", 100, true, true, "MC", " ");
            grvNguoidung.AddColumns("TextBox", "HoTen", "Họ tên", 300, true, true, "ML", " ");
            grvNguoidung.AddColumns("TextBox", "MatKhau", "Mật Khẩu", 265, true, true, "ML", " ");
            grvNguoidung.AddColumns("TextBox", "TenQuyen", "Quyền ", 265, true, true, "ML", " ");
        }

        private void cbbMaNv_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMatKhau.Text == "")
            {
                MessageBox.Show("bạn phải nhập mật khẩu");
                txtMatKhau.Focus();
                return;
            }
            if (nd.Sua(TaoNV()))
            {
                MessageBox.Show("Bạn đã sửa thành công");
                grvNguoidung.DataSource = nd.Select_ALL();
            }
            else
            {
                MessageBox.Show("Bạn chưa sửa được");
            }

        }
        NhanVienDAL TaoNV()
        {
            NhanVienDAL nv = new NhanVienDAL();
            nv.MaNv = ((NhanVienDAL)cbbMaNv.SelectedItem).MaNv;
            nv.Quyen = ((DangNhapDLL)cbbQuyen.SelectedItem).MaQuyen;
            nv.Pass = txtMatKhau.Text;
            return nv;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbbMaNv_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

       

    }
}
